package net.uiiang.android.alkaid.utils;

import java.util.ArrayList;

import org.apache.commons.io.FilenameUtils;

public class Utils {

    public static final int INDEX_NOT_FOUND = -1;

    public static boolean contains(String string, String[] strArray) {
        if (string == null || strArray == null) {
            return false;
        }
        if (string.trim().length() < 1 || strArray.length < 1) {
            return false;
        }
        for (String string2 : strArray) {
            if (string2 != null && string2.trim().equals(string.trim())) {
                return true;
            }
        }
        return false;
    }

    public static int indexOf(int[] array, int valueToFind, int startIndex) {
        if (array == null) {
            return INDEX_NOT_FOUND;
        }
        if (startIndex < 0) {
            startIndex = 0;
        }
        for (int i = startIndex; i < array.length; i++) {
            if (valueToFind == array[i]) {
                return i;
            }
        }
        return INDEX_NOT_FOUND;
    }

    public static int indexOf(int[] array, int valueToFind) {
        return indexOf(array, valueToFind, 0);
    }

    public static boolean contains(int[] array, int valueToFind) {
        return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
    }

    public static String shortenPath(String path) {
        String tmpPath = path;
        String[] tmpArray = split(tmpPath, '/');
        String retPath = "";
        if (tmpArray[0].length() < 10) {
            retPath = tmpArray[0];
        } else {
            retPath = tmpArray[0].substring(0, 10);
        }
        retPath += "/";
        for (int i = 1; i < tmpArray.length - 1; i++) {
            retPath += overlay(tmpArray[i], "...", 1, tmpArray[i].length())
                    + "/";
        }
        if (FilenameUtils.removeExtension(FilenameUtils.getName(tmpPath))
                .length() > 4) {
            retPath += overlay(tmpArray[tmpArray.length - 1], "...", 4,
                    tmpArray[tmpArray.length - 1].indexOf("."));
        } else {
            retPath += tmpArray[tmpArray.length - 1];
        }
        return retPath;
    }

    public static String[] split(String str, char separatorChar) {
        return splitWorker(str, separatorChar, false);
    }

    public static final String[] EMPTY_STRING_ARRAY = new String[0];

    private static String[] splitWorker(String str, char separatorChar,
            boolean preserveAllTokens) {
        // Performance tuned for 2.0 (JDK1.4)

        if (str == null) {
            return null;
        }
        int len = str.length();
        if (len == 0) {
            return EMPTY_STRING_ARRAY;
        }
        ArrayList list = new ArrayList();
        int i = 0, start = 0;
        boolean match = false;
        boolean lastMatch = false;
        while (i < len) {
            if (str.charAt(i) == separatorChar) {
                if (match || preserveAllTokens) {
                    list.add(str.substring(start, i));
                    match = false;
                    lastMatch = true;
                }
                start = ++i;
                continue;
            }
            lastMatch = false;
            match = true;
            i++;
        }
        if (match || (preserveAllTokens && lastMatch)) {
            list.add(str.substring(start, i));
        }
        return (String[]) list.toArray(new String[list.size()]);
    }

    public static final String EMPTY = "";

    public static String overlay(String str, String overlay, int start, int end) {
        if (str == null) {
            return null;
        }
        if (overlay == null) {
            overlay = EMPTY;
        }
        int len = str.length();
        if (start < 0) {
            start = 0;
        }
        if (start > len) {
            start = len;
        }
        if (end < 0) {
            end = 0;
        }
        if (end > len) {
            end = len;
        }
        if (start > end) {
            int temp = start;
            start = end;
            end = temp;
        }
        return new StringBuffer(len + start - end + overlay.length() + 1)
                .append(str.substring(0, start)).append(overlay).append(
                        str.substring(end)).toString();
    }

}
